package data_management;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;


public class PermissionManager {

	private static final PermissionManager permissionInstance = new PermissionManager();
	
	private Vector<UserData> users;
	public User desktopUser = null;
	Connection conn;
	
	public void initialize() {
		conn = InvDBManager.getInstance().getConnection();
				
		users = new Vector<UserData>();
		
		try {
			Statement stat = conn.createStatement();
		    ResultSet rs = stat.executeQuery("select * from login;");
		    
		    while (rs.next()) {
		    	UserData newUser = new UserData();
		    	newUser.username = rs.getString("username");
		    	newUser.password = rs.getString("password");
		    	newUser.userStatus = UserStatus.parseStatus(rs.getString("status"));
		    	users.add(newUser);
		    }
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//@ public invariant users != null;
	//@ public invariant permissionInstance != null;
	
	/**
	 * Attemps to login a user with the given username and password. If the
	 * username or password is incorrect, then a null User will be returned.
	 * Otherwise, a user object is created and is returned.
	 * @param username The username to login with
	 * @param password The password to login with
	 */
	/*@
	 @ requires username != null;
	 @ requires password != null;
	 @ 
	@*/
	public User loginUser(String username, String password) {
		
		for (UserData userData : users) {
			if (username.equals(userData.username) && password.equals(userData.password)) {
				// credentials are good!
				User newUser = new User();
				newUser.name = username;
				newUser.status = userData.userStatus;
				desktopUser = newUser;
				return newUser;
			}
		}
		
		// invalid credentials, return null
		
		System.out.println("Invalid user credentials!");
		return null;
	}
	
	/**
	 * Will check whether or not the user has permission to start a particular
	 * Activity.
	 * @param activity The activity the user wishes to perform in
	 * @param user The user to check permissions against
	 * @return true if the user has permission, false otherwise
	 */
	/*@
	 @ requires user != null;
	 @ 
	@*/
	public boolean hasPermission(Activity activity, User user) {
		
		
		switch (activity) {
		case QUERY: return true;
		case REMOVE_CATEGORY:
		case ADD_CATEGORY:
			if (user.status == UserStatus.ADMIN )
				return true;
			break;
		
			
		case ADD_ENTRY:
		case MODIFY_ENTRY:
		case REMOVE_ENTRY: 
			if (user.status == UserStatus.ADMIN || user.status == UserStatus.DROID_ADMIN)
				return true;
			break;
			
		
			
		case SALE: 
			if (user.status == UserStatus.CLERK || user.status == UserStatus.ADMIN)
				return true;
			break;
		}
		return false;
	}
	
	
	
	/**
	 * Gets a currently running instance of this class
	 */
	/*@
	 @ ensures \result != null;
	 @ 
	@*/
	public static PermissionManager getInstance() {
		return permissionInstance;
	}
	
	
	private class UserData {
		
		String username;
		String password;
		UserStatus userStatus;
		
		//@ public invariant username != null;
		//@ public invariant password != null;
	}
}
